package com.tienon.message.mq;

import java.io.IOException;
import java.util.List;
import java.util.Random;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.tienon.message.entity.Push_App;

/**
 * 单队列管理器单队列
 * 
 * @author xiuye
 *
 */
@Component
@Scope
public class SendDataToMQ {
	private static Logger log = Logger.getLogger(SendDataToMQ.class);
	private static MQQueue[] queues = new MQQueue[30];
	private static MQQueueManager manager = null;
	private MQQueue queue = null;
	
	private Random rand = new Random();
	
	static {
		MQEnvironment.hostname = "localhost";
		MQEnvironment.port = 1417;
		MQEnvironment.CCSID = 1381;
		MQEnvironment.channel = "Server";// 服务器通道

		try {
			manager = new MQQueueManager("AppMsg");
			//queues必须在上面否则报错
			for(int i = 0;i < queues.length ;i++ ){
				queues[i] =  manager.accessQueue("SendMsg",MQC.MQOO_OUTPUT);
			}
			
		} catch (MQException e) {
			
			e.printStackTrace();
		}
		
	}
	


	/**
	 * 每次首次使用必须调用
	 * 
	 * @param qMgr
	 * @param q
	 * @throws MQException
	 */
	public void openMQ() throws MQException {
		int index = rand.nextInt(queues.length);
		queue = queues[index];
	}

	
	
	@SuppressWarnings("deprecation")
	public <T> void sendMsg(T data) throws MQException, IOException {

		MQMessage msg = new MQMessage();

		msg.writeObject(data);

		queue.put(msg);
		// manager.commit();

		if (data instanceof Push_App) {
			Push_App app = (Push_App) data;
			if (data instanceof Push_App) {
				
				log.info("线程:" + Thread.currentThread().getName() + " 准备发送数据: "
						+ " 批次号:" + app.getBatId() + "流水号:" + app.getSerial()
						+ " 内容:" + app.getMsg() + " 消息的优先级:" + app.getPrior()
						+ " 预计发送数据到MQ");
			}
		}

	}

	public boolean isMQNull(){
		
		if(manager == null || queue == null){
			return true;
		}
		else{
			return false;
		}
		
		
	}
	
	public boolean isManagerConnectedAndOpen(){
		
		return manager.isConnected && manager.isOpen ;
		
	}
	
	public boolean isQueueOpen(){
		
		return queue.isOpen;
		
	}
	
	public <T> void sendMsgs(List<T> datas) throws MQException, IOException {

	
		for (T data : datas) {
			
			
			
			
			MQMessage msg = new MQMessage();
			msg.writeObject(data);
			queue.put(msg);
			// manager.commit();
			if (data instanceof Push_App) {
				Push_App app = (Push_App) data;
				log.info("线程:" + Thread.currentThread().getName() + "准备发送数据: "
						+ " 批次号:" + app.getBatId() + "流水号:" + app.getSerial()
						+ " 内容:" + app.getMsg() + " 消息的优先级:" + app.getPrior()
						+ "  预计插入数据到MQ");
			}
		}

	}

	/**
	 * 必须要调用
	 * 
	 * @throws MQException
	 */

	public void commit() throws MQException{
		
		/*
		 * 把commit交代在最后是为了， 好与数据库操作的事务 起到一致的作用
		 */
		manager.commit();// 不要重复commit，否者会报错的
		
	}
	
	public void closeMQ() throws MQException {

		
		queue.close();
		manager.disconnect();
		queue = null;
		manager = null;

	}
}
